﻿using WeDonekRpc.Helper;

namespace Basic.UserFormService.Calculate.Extend
{
    public static class ArrayExtend
    {
        public static bool CheckIsTrue ( object[] array )
        {
            if ( array.IsNull() )
            {
                return false;
            }
            else if ( CurState.Index.HasValue )
            {
                return Convert.ToBoolean(array[CurState.Index.Value]) == true;
            }
            return false;
        }
        public static bool CheckIsFalse ( object[] array )
        {
            if ( array.IsNull() )
            {
                return false;
            }
            else if ( CurState.Index.HasValue )
            {
                return Convert.ToBoolean(array[CurState.Index.Value]) == false;
            }
            return false;
        }
        /// <summary>
        /// 将至少两个数组中的成员相互计算乘法
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object[] ArrayMultiply ( params object[][] data )
        {
            if ( data.IsNull() )
            {
                return Array.Empty<object>();
            }
            else if ( data.Length == 1 )
            {
                return data[0];
            }
            object[] one = data[0];
            if ( data.Length == 2 )
            {
                object[] two = data[1];
                return one.ConvertAll<object, object>(( c, i ) =>
                {
                    if ( c == null || two[i] == null )
                    {
                        return null;
                    }
                    return Convert.ToDecimal(c) * Convert.ToDecimal(two[i]);
                });
            }
            else
            {
                int len = data.Length;
                return one.ConvertAll<object, object>(( c, i ) =>
                {
                    if ( c == null )
                    {
                        return null;
                    }
                    decimal num = Convert.ToDecimal(c);
                    for ( int k = 1 ; k < len ; k++ )
                    {
                        object val = data[k][i];
                        if ( val != null )
                        {
                            num += Convert.ToDecimal(data[k][i]);
                        }
                    }
                    return num;
                });
            }
        }

        /// <summary>
        /// 根据结果数组合并数组 check= true 取为真的数据 false 取为假的数据
        /// </summary>
        /// <param name="check"></param>
        /// <param name="tru"></param>
        /// <param name="fal"></param>
        /// <returns></returns>
        public static object[] IsTrue ( object[] check, object[] tru, params object[] fal )
        {
            if ( fal.IsNull() )
            {
                return check.ConvertAll(( a, i ) =>
                {
                    return Convert.ToBoolean(a) ? tru[i] : null;
                });
            }
            return check.ConvertAll(( a, i ) =>
            {
                return Convert.ToBoolean(a) ? tru[i] : fal?[i];
            });
        }
        /// <summary>
        /// 根据结果数组合并数组 check=false 取为真的数据 check=true 取为假的数据
        /// </summary>
        /// <param name="check"></param>
        /// <param name="tru"></param>
        /// <param name="fal"></param>
        /// <returns></returns>
        public static object[] IsFalse ( object[] check, object[] tru, params object[] fal )
        {
            if ( fal.IsNull() )
            {
                return check.ConvertAll(( a, i ) =>
                {
                    return Convert.ToBoolean(a) == false ? tru[i] : null;
                });
            }
            return check.ConvertAll(( a, i ) =>
            {
                return Convert.ToBoolean(a) == false ? tru[i] : fal?[i];
            });
        }
    }
}
